<?php defined('SYSPATH') or die('No direct access allowed.');

class Controller_News extends Controller_ClientTemplate
{
    
     public function action_index()
    {       
        $href = '/'.Request::$current->param('href', '');
        
        if($href == '/'){          
            $count = DB::query(Database::SELECT, "SELECT COUNT(*) AS `count` FROM `news`")
                        ->execute()
                        ->get('count');
                    
            $pagination = Pagination::factory(array('total_items' => $count));
            
            $path = Kohana::$config->load('news')->get('image_path');
                    
            $news = DB::query(Database::SELECT, "SELECT n.*, CONCAT(:path, REPLACE(ni.file,'.','_min.')) as src
                                                 FROM `news` as n
                                                 LEFT JOIN `news_images` as ni
                                                 ON n.id=ni.news_id
                                                 GROUP BY n.id
                                                 ORDER BY n.created DESC
                                                 LIMIT :offset, :limit")
                        ->param(':path', $path)
                        ->param(':offset', $pagination->offset)
                        ->param(':limit', $pagination->items_per_page)
                        ->as_object()
                        ->execute();
                 
            $page_links = $pagination->render();
            
            $this->template->title = "Новости";
            
            $news_content = View::factory('news/news')
                                 ->set('news', $news)
                                 ->set('page_links', $page_links)
                                 ->render();
            
            $this->template->content = View::factory('content')
                                            ->set('title', "Новости")
                                            ->set('content', $news_content);
                                        
            
        } else {            
            $news = DB::query(Database::SELECT, "SELECT * FROM `news` WHERE `href`=:href LIMIT 1")
                        ->param(':href', $href)
                        ->as_object()
                        ->execute();
                        
            if($news->count()){
                $news = $news->current();
                $this->template->title = $news->html_title;
                $this->template->description = $news->html_description;
                $this->template->keywords = $news->html_keyword;
                
                $this->template->content = View::factory('content')
                                                ->set('title', $news->html_title)
                                                ->set('content', $news->content);              
            } else {
                $this->auto_render = false;
                $this->response->status(404);
                $this->response->body(Request::factory('error/404')->execute());
            }
        }
    }
    
    public function action_block()
    {
        $this->auto_render = false;
        
        $path = Kohana::$config->load('news')->get('image_path');
        
        $news = DB::query(Database::SELECT, "SELECT n.*, CONCAT(:path,REPLACE(ni.file,'.','_min.')) as src
                                             FROM `news` as n
                                             LEFT JOIN `news_images` as ni
                                             ON n.id=ni.news_id
                                             GROUP BY n.id
                                             ORDER BY n.created DESC
                                             LIMIT 3")
                    ->param(':path', $path)
                    ->as_object()
                    ->execute();                
        
        $body = View::factory('news/block')
                     ->set('news', $news)
                     ->render();
        
        $this->response->body($body);
        
    }

}